package com.slashmanx.goals2u;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;

public class TabMain extends TabActivity 
{

	public GoalsDatabaseManager db;
	public String type = "all";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tablayout);
        db = new GoalsDatabaseManager(this);
        
        
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        Bundle extras = getIntent().getExtras();
        if(extras !=null) 
        {
        	type = extras.getString("type");
        }
        intent = new Intent().setClass(this, PremierTab.class);
        intent.putExtra("type", type);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("premier").setIndicator("",
                          res.getDrawable(R.drawable.premier_logo))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, AirtricityTab.class);
        intent.putExtra("type", type);
        spec = tabHost.newTabSpec("airtricity").setIndicator("",
                          res.getDrawable(R.drawable.airtricity_logo))
                      .setContent(intent);
        tabHost.addTab(spec);
        

        tabHost.setCurrentTab(0);
    }
    
    public boolean onCreateOptionsMenu(Menu menu) { 
    	menu.add(1,1,1,"Settings");
        menu.findItem(1).setIcon(android.R.drawable.ic_menu_manage);
        return super.onCreateOptionsMenu(menu);  
      } 
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
            	Intent pIntent = new Intent(this, Preferences.class);
            	startActivity(pIntent);
            	break;
        }
        return true;
    }
    
    public void onDestroy()
    {
    	super.onDestroy();
    	if(db != null)
		{
			db.close();
		}
		db = null;
    }
    
    public void onPause()
    {
    	super.onPause();
    	if(db != null)
		{
			db.close();
		}
		db = null;
    }
    
    public void onResume()
    {
    	super.onResume();
    	if(db != null)
		{
			db.close();
		}
    	db = new GoalsDatabaseManager(this); 
    }

	
}
